﻿// ----------------------------------------------------------------------
// <author name="Moses Kehengu"/>
// <copyright file="FacilityDetails.cs" company="Futures Group International">
//     Copyright statement. All right reserved
// </copyright>
//
// ------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using GenericTools;

namespace ReportingManagement
{
    public enum FacilityStatus
    {
        ACTIVE = 1,
        INACTIVE = 2
    }

    public class Facility
    {
        #region Constants
        public const int FACILITY_STATUS_ACTIVE = 1;
        public const int FACILITY_STATUS_INACTIVE = 2;
        #endregion

        #region Class Variables
        private string oldCode = null;
        private string code = null;
        private string name = null;
        private string shortname = null;
        private string internalName = null;
        private int region = -1;
        private int district = -1;
        
        private bool provideLD = false;
        private bool provideANC = false;
        private bool provideHEID = false;
        private bool provideProphylaxis = false;
        
        private DateTime LDStartDateTime;
        private DateTime ANCStartDateTime;
        private DateTime HEIDStartDateTime;
        private DateTime ProphylaxisDateTime;

        private DateTime activityDateTime;
        private bool actived = false;
        
        private string description = null;
        private string address = null;
        private string principalContact = null;
        private string remarks = null;
        
        //private Dictionary<int, Dictionary<DateTime, bool>> expectedReports = new Dictionary<int,Dictionary<DateTime,bool>>();
        private DataTable expectedReports = null;
        private bool isValid = false;
        private bool canBeDeleted = false;

        private bool trained = false;
        private bool provideDBS = false;

        int actionUserId = -1;
        int facilityType = -1;

        private int supportingPartnerId = -1;
        private int _status;
        #endregion        
        
        #region Constructors
        public Facility(int actionUserId)
        {           
            DateTime defaultTime = DateTime.Now;
            LDStartDateTime = defaultTime;
            ANCStartDateTime = defaultTime;
            HEIDStartDateTime = defaultTime;
            ProphylaxisDateTime = defaultTime;

            activityDateTime = defaultTime;
            actived = true;

            //expectedReports = new Dictionary<int, DateTime>();
            expectedReports = new DataTable();
            DataColumn column = new DataColumn("ReportId", Type.GetType("System.Int32"));
            expectedReports.Columns.Add(column);
            column = new DataColumn("ReportingStartDate", Type.GetType("System.DateTime"));
            expectedReports.Columns.Add(column);
            column = new DataColumn("DirectEntry", Type.GetType("System.Boolean"));
            expectedReports.Columns.Add(column);

            isValid = false;
            canBeDeleted = false;

            this.actionUserId = actionUserId;
            _status = FACILITY_STATUS_ACTIVE;
        }

        public Facility(string facilityCode, int actionUserId)
        {
            this.actionUserId = actionUserId;
            DataTable dataTable = Utilities.GetFacilityDetails(facilityCode);
            if (dataTable.Rows.Count != 1)
            {
                isValid = false;
                return;
            }

            DataRow row = dataTable.Rows[0];
            trained = bool.Parse(row["Trained"].ToString());
            oldCode = code = row["FacCode"].ToString();
            name = row["FacName"].ToString();
            shortname = row["FacShortName"].ToString();
            internalName = row["InternalName"].ToString();
            region = int.Parse(row["Region"].ToString());
            district = int.Parse(row["District"].ToString());
            
            provideLD = bool.Parse(row["LDStatus"].ToString());
            provideANC = bool.Parse(row["ANC"].ToString());
            provideHEID = bool.Parse(row["HEID"].ToString());
            provideProphylaxis = bool.Parse(row["Prophylaxis"].ToString());
            
            description = row["Description"].ToString();
            address = row["Address"].ToString();
            principalContact = row["PrincipalContact"].ToString();
            remarks = row["Remarks"].ToString();

            LDStartDateTime = DateTime.Parse(row["LDStartDate"].ToString());
            ANCStartDateTime = DateTime.Parse(row["ANCStartDate"].ToString());
            HEIDStartDateTime = DateTime.Parse(row["HEIDStartDate"].ToString());
            ProphylaxisDateTime = DateTime.Parse(row["ProphylaxisStartDate"].ToString());

            activityDateTime = DateTime.Parse(row["ActiveDate"].ToString());
            actived = bool.Parse(row["Activated"].ToString());
            facilityType = Int32.Parse(row["FacilityType"].ToString());
            provideDBS = bool.Parse(row["ProvideDBS"].ToString());
            supportingPartnerId = Int32.Parse(row["SupportingPartnerId"].ToString());
            if (!int.TryParse(row["Status"].ToString(), out _status)) _status = FACILITY_STATUS_ACTIVE;

            //expectedReports = new Dictionary<int, Dictionary<DateTime, bool>>();
            expectedReports = new DataTable();
            DataColumn column = new DataColumn("ReportId", Type.GetType("System.Int32"));
            expectedReports.Columns.Add(column);
            column = new DataColumn("ReportingStartDate", Type.GetType("System.DateTime"));
            expectedReports.Columns.Add(column);
            column = new DataColumn("DirectEntry", Type.GetType("System.Boolean"));
            expectedReports.Columns.Add(column);

            dataTable = Utilities.GetFacilityExpectedReports(facilityCode);
            foreach (DataRow r in dataTable.Rows)
            {
                int reportID = Int32.Parse(r["ReportID"].ToString());
                DateTime reportingStartDate = DateTime.Parse(r["ReportingStartDate"].ToString());
                bool directEntry = Boolean.Parse(r["DirectEntry"].ToString());
                
                DataRow expectedReport = expectedReports.NewRow();
                expectedReport["ReportID"] = reportID;
                expectedReport["ReportingStartDate"] = reportingStartDate;
                expectedReport["DirectEntry"] = directEntry;
                expectedReports.Rows.Add(expectedReport);

                //expectedReports[reportID] = reportingStartDate;
            }
            this.canBeDeleted = Utilities.FacilityDetailsCanDeleteFacility(this.oldCode);
            isValid = true;
        }
        #endregion

        #region Public Properties
        public string Code
        {
            get
            {
                return code;
            }
            set
            {
                code = value;
            }
        }

        public string OriginalCode
        {
            get
            {
                return oldCode;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public string ShortName
        {
            get
            {
                return shortname;
            }
            set
            {
                shortname = value;
            }
        }

        public int RegionID
        {
            get
            {
                return region;
            }
            set
            {
                region = value;
            }
        }

        public int DistrictID
        {
            get
            {
                return district;
            }
            set
            {
                district = value;
            }
        }
        
        public bool ProvideLD
        {
            get
            {
                return provideLD;
            }
            set
            {
                provideLD = value;
            }
        }

        public bool ProvideANC
        {
            get
            {
                return provideANC;
            }
            set
            {
                provideANC = value;
            }
        }

        public bool ProvideHEID
        {
            get
            {
                return provideHEID;
            }
            set
            {
                provideHEID = value;
            }
        }

        public bool ProvideProphylaxis
        {
            get
            {
                return provideProphylaxis;
            }
            set
            {
                provideProphylaxis = value;
            }
        }

        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
            }
        }

        public string Address
        {
            get
            {
                return address;
            }
            set
            {
                address = value;
            }
        }

        public string PrincipalContact
        {
            get
            {
                return principalContact;
            }
            set
            {
                principalContact = value;
            }
        }

        public string Remarks
        {
            get
            {
                return remarks;
            }
            set
            {
                remarks = value;
            }
        }
        
        public DateTime LDStartDate
        {
            get
            {
                return LDStartDateTime;
            }
            set
            {
                LDStartDateTime = value;
            }
        }

        public DateTime ANCStartDate
        {
            get
            {
                return ANCStartDateTime;
            }
            set
            {
                ANCStartDateTime = value;
            }
        }

        public DateTime HEIDStartDate
        {
            get
            {
                return HEIDStartDateTime;
            }
            set
            {
                HEIDStartDateTime = value;
            }
        }

        public DateTime ProphylaxisStartDate
        {
            get
            {
                return ProphylaxisDateTime;
            }
            set
            {
                ProphylaxisDateTime = value;
            }
        }

        public DateTime ActivityDate
        {
            get
            {
                return activityDateTime;
            }
            set
            {
                activityDateTime = value;
            }
        }

        public bool Actived
        {
            get
            {
                return this.actived;
            }
            set
            {
                this.actived = value;
            }
        }

        public DataTable ExpectedReports
        {
            get
            {
                return expectedReports;
            }
        }

        public bool IsValid
        {
            get
            {
                return isValid;
            }
        }

        public bool CanBeDeleted
        {
            get
            {
                return this.canBeDeleted;
            }
        }

        public bool Trained
        {
            get
            {
                return this.trained;
            }
            set
            {
                this.trained = value;
            }
        }

        public bool ProvideDBS
        {
            get
            {
                return this.provideDBS;
            }
            set
            {
                this.provideDBS = value;
            }
        }

        public string InternalName
        {
            get
            {
                return this.internalName;
            }
            set
            {
                this.internalName = value;
            }
        }

        public int FacilityType
        {
            get
            {
                return this.facilityType;
            }
            set
            {
                this.facilityType = value;
            }
        }

        public int SupportingPartnerId
        {
            set
            {
                supportingPartnerId = value;
            }
            get
            {
                return supportingPartnerId;
            }
        }

        public int Status
        {
            get { return _status; }
            set { _status = value; }
        }
        #endregion

        #region Public Methods
        public bool Save()
        {
            if (isValid) Update();
            else Insert();
            SaveExpectedReports();
            return true;
        }
        #endregion

        #region Private Methods
        private bool Insert()
        {
            bool okToContine = Utilities.FacilityDetailsInsert2(code, name, internalName, shortname, region, district, description,
                address, principalContact, remarks, LDStartDateTime, ProphylaxisDateTime,
                ANCStartDateTime, HEIDStartDate, provideANC, provideHEID, provideLD, provideProphylaxis,
                activityDateTime, actived, trained, provideDBS, facilityType, supportingPartnerId, 
                _status, actionUserId);
            return okToContine;
        }

        private bool Update()
        {
            bool okToContine = Utilities.FacilityDetailsUpdate2(oldCode, code, name, internalName, shortname, region, district, description,
                address, principalContact, remarks, LDStartDateTime, ProphylaxisDateTime,
                ANCStartDateTime, HEIDStartDate, provideANC, provideHEID, provideLD, provideProphylaxis,
                activityDateTime, actived, trained, provideDBS, facilityType, supportingPartnerId, actionUserId);

            if (okToContine)
            {
                oldCode = code;
                return true;
            }
            else return false;
        }

        private void SaveExpectedReports()
        {
            Utilities.FacilityDetailsSaveExpectedReports(code, expectedReports);
        }
        #endregion

        #region Public Static Methods
        public static bool Delete(string facilityCode, int actionUserId)
        {
            Facility facility = new Facility(facilityCode, actionUserId);
            if (facility.CanBeDeleted)
            {
                bool status = Utilities.FacilityDetailsDelete(facilityCode);
                return status;
            }
            else
            {
                facility.Actived = false;
                facility.remarks = "Facility is inactive due to delete operation initiated by System Administrator";
                facility.ActivityDate = DateTime.Now;
                facility.Save();
                return true;
            }
        }

        public static bool Delete(Facility facility)
        {
            if (facility.CanBeDeleted)
            {
                bool status = Utilities.FacilityDetailsDelete(facility.OriginalCode);
                return status;
            }
            else
            {
                facility.Actived = false;
                facility.remarks = "Facility is inactive due to delete operation initiated by System Administrator";
                facility.ActivityDate = DateTime.Now;
                facility.Save();
                return true;
            }
        }
        #endregion
    }
}
